home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / callproc.c < prev    next >
C/C++ Source or Header  |  1991-12-02  |  14KB  |  564 lines

  1. /* Synchronous subprocess invocation for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /**
  21.  **  (sjk)++ The space to build a temporary file name.
  22.  **          Dev null installed flag, do not install /dev/null
  23.  **          until emacs is run in interactive mode (not in dumps).
  24.  **/
  25. #if defined(atarist)
  26. char   at_tmpname[32];
  27. static short null_dev_inst = 0;
  28. #endif
  29.  
  30. #include <signal.h>
  31.  
  32. #include "config.h"
  33.  
  34. #include <sys/types.h>
  35. #define PRIO_PROCESS 0
  36. #include <sys/file.h>
  37. #ifdef USG5
  38. #include <fcntl.h>
  39. #endif
  40.  
  41. #ifndef O_RDONLY
  42. #define O_RDONLY 0
  43. #endif
  44.  
  45. #ifndef O_WRONLY
  46. #define O_WRONLY 1
  47. #endif
  48.  
  49. #include "lisp.h"
  50. #include "commands.h"
  51. #include "buffer.h"
  52. #include "paths.h"
  53.  
  54. #define max(a, b) ((a) > (b) ? (a) : (b))
  55.  
  56. Lisp_Object Vexec_path, Vexec_directory;
  57.  
  58. Lisp_Object Vshell_file_name;
  59.  
  60. #ifndef MAINTAIN_ENVIRONMENT
  61. /* List of strings to append to front of environment of
  62.    all subprocesses when they are started.  */
  63.  
  64. Lisp_Object Vprocess_environment;
  65. #endif
  66.  
  67. #ifdef BSD4_1
  68. /* Set nonzero when a synchronous subprocess is made,
  69.    and set to zero again when it is observed to die.
  70.    We wait for this to be zero in order to wait for termination.  */
  71. int synch_process_pid;
  72. #endif /* BSD4_1 */
  73.  
  74. Lisp_Object
  75. call_process_cleanup (fdpid)
  76.      Lisp_Object fdpid;
  77. {
  78.   register Lisp_Object fd, pid;
  79.   fd = Fcar (fdpid);
  80.   pid = Fcdr (fdpid);
  81. /**
  82.  **  (sjk)++ We close the file in the function from where the external
  83.  **          process was creaed. 
  84.  **/
  85. #if defined(atarist)
  86.   if (XFASTINT(fd)>0)  close (XFASTINT (fd));
  87. #else
  88.   close (XFASTINT (fd));
  89.   kill (XFASTINT (pid), SIGKILL);
  90. #endif
  91.   return Qnil;
  92. }
  93.  
  94. #ifdef VMS
  95. extern noshare char **environ;
  96. #else
  97. extern char **environ;
  98. #endif
  99.  
  100. DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
  101.   "Call PROGRAM in separate process.\n\
  102. Program's input comes from file INFILE (nil means /dev/null).\n\
  103. Insert output in BUFFER before point; t means current buffer;\n\
  104.  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
  105. Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  106. Remaining arguments are strings passed as command arguments to PROGRAM.\n\
  107. This function waits for PROGRAM to terminate;\n\
  108. if you quit, the process is killed.")
  109.   (nargs, args)
  110.      int nargs;
  111.      register Lisp_Object *args;
  112. {
  113.   Lisp_Object display, buffer, path;
  114.   int fd[2];
  115.   int filefd;
  116.   register int pid;
  117.   char buf[1024];
  118.   int count = specpdl_ptr - specpdl;
  119.   register unsigned char **new_argv
  120.     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
  121.   struct buffer *old = current_buffer;
  122.  
  123. /**
  124.  **  (sjk)++ Install bammi's plug&play null data sink.
  125.  **/
  126. #if defined(atarist)
  127.   if (!null_dev_inst)
  128.     { install_null();
  129.       null_dev_inst = 1;
  130.     }
  131. #endif
  132.  
  133.   CHECK_STRING (args[0], 0);
  134.  
  135.   if (nargs <= 1 || NULL (args[1]))
  136. #ifdef VMS
  137.     args[1] = build_string ("NLA0:");
  138. #else
  139.     args[1] = build_string ("/dev/null");
  140. #endif /* not VMS */
  141.   else
  142.     args[1] = Fexpand_file_name (args[1], current_buffer->directory);
  143.  
  144.   CHECK_STRING (args[1], 1);
  145.  
  146.   {
  147.     register Lisp_Object tem;
  148.     buffer = tem = args[2];
  149.     if (nargs <= 2)
  150.       buffer = Qnil;
  151.     else if (!(EQ (tem, Qnil) || EQ (tem, Qt)
  152.            || XFASTINT (tem) == 0))
  153.       {
  154.     buffer = Fget_buffer (tem);
  155.     CHECK_BUFFER (buffer, 2);
  156.       }
  157.   }
  158.  
  159.   display = nargs >= 3 ? args[3] : Qnil;
  160.  
  161.   {
  162.     register int i;
  163.     for (i = 4; i < nargs; i++)
  164.       {
  165.     CHECK_STRING (args[i], i);
  166.     new_argv[i - 3] = XSTRING (args[i])->data;
  167.       }
  168.     /* Program name is first command arg */
  169.     new_argv[0] = XSTRING (args[0])->data;
  170.     new_argv[i - 3] = 0;
  171.   }
  172.  
  173. /**
  174.  **  (sjk)++ Open temp file in R/W mode on the ST...
  175.  **/
  176. #if defined(atarist)
  177.   filefd = open (XSTRING (args[1])->data, O_RDWR , 666);
  178. #else
  179.   filefd = open (XSTRING (args[1])->data, O_RDONLY, 0);
  180. #endif
  181.  
  182.   if (filefd < 0)
  183.     {
  184.       report_file_error ("Opening process input file", Fcons (args[1], Qnil));
  185.     }
  186.   /* Search for program; barf if not found.  */
  187. /**
  188.  **  (sjk)++ When searching for executables check some obvious extensions.
  189.  **/
  190. #if defined(atarist)
  191.   openp (Vexec_path, args[0], ":ttp:tos:prg", &path, 1);
  192. #else
  193.   openp (Vexec_path, args[0], "", &path, 1);
  194. #endif
  195.  
  196.   if (NULL (path))
  197.     {
  198.       close (filefd);
  199.       report_file_error ("Searching for program", Fcons (args[0], Qnil));
  200.     }
  201.   new_argv[0] = XSTRING (path)->data;
  202.  
  203.   if (XTYPE (buffer) == Lisp_Int)
  204. #ifdef VMS
  205.     fd[1] = open ("NLA0:", 0), fd[0] = -1;
  206. #else
  207.     fd[1] = open ("/dev/null", O_WRONLY), fd[0] = -1;
  208. #endif /* not VMS */
  209.   else
  210.     {
  211.       pipe (fd);
  212. #if 0
  213.       /* Replaced by close_process_descs */
  214.       set_exclusive_use (fd[0]);
  215. #endif
  216.     }
  217.  
  218.   {
  219.     /* child_setup must clobber environ in systems with true vfork.
  220.        Protect it from permanent change.  */
  221.     register char **save_environ = environ;
  222.     register int fd1 = fd[1];
  223.     char **env;
  224.  
  225. #ifdef MAINTAIN_ENVIRONMENT
  226.     env = (char **) alloca (size_of_current_environ ());
  227.     get_current_environ (env);
  228. #else
  229.     env = environ;
  230. #endif /* MAINTAIN_ENVIRONMENT */
  231.  
  232.     pid = vfork ();
  233. #ifdef BSD4_1
  234.     /* cause SIGCHLD interrupts to look for this pid. */
  235.     synch_process_pid = pid;
  236. #endif /* BSD4_1 */
  237.  
  238.     if (pid == 0)
  239.       {
  240. /**
  241.  **  (sjk)++ Avoid some excess file closes.
  242.  **/
  243. #if !defined(atarist)
  244.     if (fd[0] >= 0)
  245.       close (fd[0]);
  246. #ifdef USG
  247. #ifdef HAVE_PTYS
  248.     setpgrp ();
  249. #endif
  250. #endif
  251. #endif /* !defined(atarist) */
  252.  
  253.     child_setup (filefd, fd1, fd1, new_argv, env);
  254.       }
  255.  
  256.     environ = save_environ;
  257.  
  258.     close (filefd);
  259.     close (fd1);
  260.   }
  261.  
  262.   if (pid < 0)
  263.     {
  264. /**
  265.  **  (sjk)++ Make sure both end of pipe get closed if vfork() fails
  266.  **/
  267. #if defined(atarist)
  268.       close (fd[1]);
  269. #endif
  270.  
  271.       close (fd[0]);
  272.       report_file_error ("Doing vfork", Qnil);
  273.     }
  274.  
  275.   if (XTYPE (buffer) == Lisp_Int)
  276.     {
  277. #ifndef subprocesses
  278.       wait_without_blocking ();
  279. #endif subprocesses
  280.       return Qnil;
  281.     }
  282.  
  283.   record_unwind_protect (call_process_cleanup,
  284.              Fcons (make_number (fd[0]), make_number (pid)));
  285.  
  286.  
  287.   if (XTYPE (buffer) == Lisp_Buffer)
  288.     Fset_buffer (buffer);
  289.  
  290.   immediate_quit = 1;
  291.   QUIT;
  292.  
  293.   {
  294.     register int nread;
  295.  
  296.     while ((nread = read (fd[0], buf, sizeof buf)) > 0)
  297.       {
  298.     immediate_quit = 0;
  299.     if (!NULL (buffer))
  300.       insert (buf, nread);
  301.     if (!NULL (display) && FROM_KBD)
  302.       redisplay_preserve_echo_area ();
  303.     immediate_quit = 1;
  304.     QUIT;
  305.       }
  306.   }
  307.  
  308. /**
  309.  **  (sjk)++ here we close the write side of the pipe. 
  310.  **/
  311. #if defined(atarist)
  312.   close(fd[0]);
  313. #endif
  314.  
  315.   /* Wait for it to terminate, unless it already has.  */
  316.   wait_for_termination (pid);
  317.  
  318.   immediate_quit = 0;
  319.  
  320.   set_buffer_internal (old);
  321.  
  322.   unbind_to (count);
  323.  
  324.   return Qnil;
  325. }
  326.  
  327. DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
  328.   3, MANY, 0,
  329.   "Send text from START to END to a process running PROGRAM.\n\
  330. Delete the text if DELETE is non-nil.\n\
  331. Put output in BUFFER, before point.  nil => discard it, t => current buffer.\n\
  332. Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  333. Remaining args are passed to PROGRAM at startup as command args.\n\
  334. This function normally waits for the process to terminate;\n\
  335. if you quit, the process is killed.")
  336.   (nargs, args)
  337.      int nargs;
  338.      register Lisp_Object *args;
  339. {
  340.   register Lisp_Object filename_string, start, end;
  341.  
  342. /**
  343.  **  (sjk)++  ask for a temp file name.
  344.  **/
  345. #if !defined(atarist)
  346.   char tempfile[20];
  347. #endif
  348. #if defined(atarist)
  349.   tmpnam(at_tmpname);
  350. #else
  351.   strc